/**
 * 
 */
package uit.comstore.model.blo;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import uit.comstore.model.entity.Account;
import uit.comstore.model.entity.Inventory;
import uit.comstore.utils.HibernateSessionFactory;

/**
 * @author XIII
 *
 */
public class AccountBLO {

	@SuppressWarnings("unchecked")
	public List<Account> list() {
		List<Account> accounts = null;
		Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession();	
		session.beginTransaction();
		accounts = (List<Account>)session.createQuery("from Account").list();
		session.getTransaction().commit();	
		return accounts;
	}
	
	@SuppressWarnings("unchecked")
	public List<Inventory> listR() {
		List<Inventory> accounts = null;
		Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession();	
		session.beginTransaction();
		accounts = (List<Inventory>)session.createQuery("from Inventory").list();
		session.getTransaction().commit();	
		return accounts;
	}
	
	@SuppressWarnings("unchecked")
	public Account getAccount(String username, String password) {
		Account account = null;
		Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Query query = session.createQuery("from Account where username = :value1 and password = :value2 and deleted = :value3");
		query.setParameter("value1", username);
		query.setParameter("value2", password);
		query.setParameter("value3", 0);
		List<Account> accountList = query.list();
		if (accountList.size() != 0) {
			account = accountList.get(0);
		}
		session.getTransaction().commit();
		return account;
	}
}
